package com.tang.generate.controller

import com.baomidou.mybatisplus.core.metadata.IPage
import com.tang.framework.core.controller.BaseController
import com.tang.framework.core.domain.HttpResult
import com.tang.common.model.req.ReqId
import com.tang.common.model.req.ReqIds
import com.tang.common.model.req.ReqString
import com.tang.common.model.req.ReqStrings
import com.tang.generate.domain.GenFieldType
import com.tang.generate.model.req.ReqGenTable
import com.tang.generate.model.req.ReqGenerate
import com.tang.generate.model.res.ResGenDetail
import com.tang.generate.model.res.ResGenTableItem
import com.tang.generate.service.IGenService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import lombok.SneakyThrows
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import jakarta.annotation.Resource

@Tag(name = "代码生成")
@RestController
@RequestMapping("/generate")
class GenController : BaseController() {


    @Resource
    private lateinit var genService: IGenService;


    @Operation(summary = "生成表的列表")
    @PostMapping("/list")
    fun list(@RequestBody reqGenTableItem: ReqGenTable): HttpResult<IPage<ResGenTableItem>> =
        HttpResult.ok(genService.list(reqGenTableItem));


    @Operation(summary = "表的信息")
    @PostMapping("/detail")
    fun detail(@RequestBody reqId: ReqId): HttpResult<ResGenDetail?> = HttpResult.ok(genService.detail(reqId));

    @Operation(summary = "导入表")
    @PostMapping("/importTable")
    fun importTable(@RequestBody reqStrings: ReqStrings): HttpResult<String?> = genService.importTable(reqStrings)


    @Operation(summary = "数据库未导入的表")
    @PostMapping("/databaseTableList")
    fun databaseTableList(@RequestBody reqGenTableItem: ReqGenTable): HttpResult<IPage<Unit>>? =
        genService.databaseTableList(reqGenTableItem)

    @Operation(summary = "修改生成表的业务信息")
    @PostMapping("/editGenTable")
    fun editGenTable(@RequestBody resGenDetail: ResGenDetail): HttpResult<String?> =
        genService.editGenTable(resGenDetail);


    @Operation(summary = "下载压缩包")
    @PostMapping("/downloadCode")
    fun downloadCode(@RequestBody reqIds: ReqIds) = genService.downloadCode(reqIds, httpServletResponse);

    @Operation(summary = "生成到本地")
    @PostMapping("/generateLocal")
    fun generateLocal(@RequestBody reqGenerate: ReqGenerate?) = genService.generateLocal(reqGenerate)

    @Operation(summary = "预览代码")
    @PostMapping("/previewCode")
    fun previewCode(@RequestBody reqIds: ReqIds): HttpResult<Map<String, String>> =
        HttpResult.ok(genService.previewCode(reqIds));


    @Operation(summary = "获取java字段类型")
    @PostMapping("/genFieldTypeList")
    fun genFieldTypeList(@RequestBody reqString: ReqString): HttpResult<List<GenFieldType>> =
        HttpResult.ok(genService.genFieldTypeList(reqString));


    @SneakyThrows
    @Operation(summary = "生成枚举代码")
    @PostMapping("/generateDictCode")
    fun generateDictCode(@RequestBody reqGenerate: ReqGenerate?) {
        genService.generateDictCode(reqGenerate, httpServletResponse)
    }

    @SneakyThrows
    @Operation(summary = "同步表")
    @PostMapping("/syncTable")
    fun syncTable(@RequestBody reqStrings: ReqStrings): HttpResult<String> = genService.syncTable(reqStrings);


    @SneakyThrows
    @Operation(summary = "创建枚举字段在数据库")
    @PostMapping("/createEnum")
    fun createEnum(@RequestBody reqId: ReqId) {
        genService.createEnum(reqId, httpServletResponse)
    }


}
